package com.google.code.seditor.db;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DaoFactory {
	
	private static Map<Context,DBHelper> mDbHelper;
	
	public static Map<String,CommonDao> daoMap=new HashMap<String,CommonDao>();
		
    public static final void close(Context mCtx) {
    	DBHelper helper=mDbHelper.remove(mCtx);
    	if(helper!=null){
    		helper.close();
    	}
    	
    }
    
	public static final void init(){
		mDbHelper =new HashMap<Context,DBHelper>();	 
	}
	
	public static final CommonDao getDao(Class clazz,Context mCtx){
		
		DBHelper dbHelper=mDbHelper.get(mCtx);
		if(dbHelper==null){
			dbHelper = new DBHelper(mCtx);
			mDbHelper.put(mCtx, dbHelper);
		}
				
		if(daoMap.containsKey(clazz.getName())){
			return daoMap.get(clazz.getName());
		}
		CommonDao dao=null;
		try {
			dao=(CommonDao) clazz.newInstance();
		} catch (Exception e) {
			Log.w("DaoFactory", e);
			return null;

		}
		dao.setDBHelper(dbHelper);
		daoMap.put(clazz.getName(), dao);
		return dao;
		
	}

}
